<?php

/***  DOCUMENTATION LAYER

Klenwell Ceo Form Element Input Text Class

Name: CeoFormElementInputText
Last Update: Sep 2007
Author: Tom at klenwell@gmail.com

DESCRIPTION
  CEO form element class for text input fields

METHODS
  MAGIC
  CeoFormElementInputText($name, $type, $label, $is_required, $maxlength, $minlength)    *php 4 constructor*
  __construct($name, $type, $label, $is_required, $maxlength, $minlength)    *php 5 constructor*
  __destruct()  
  
  PUBLIC
  
  PRIVATE
  _is_valid_url_strict($url)
  _set_session_data()
  _get_session_data()
  _has_session_data()
  _set_filename()
  _set_dirpath()
  
USAGE
  $Class = new ClassClass();
  $Class->print_r('hello world']);

NOTES

______________________________________________________________________________*/

// Load File of Base Class


// CeoFormElementInputText
/*____________________________________________________________________________*/
class CeoFormElementInputText extends CeoFormElement
{
/* PUBLIC PROPERTIES */
var $debug = 0;
var $class_name = __CLASS__;
var $oid = '';
var $DS = DIRECTORY_SEPARATOR;

// control arrays
var $VALID = array();

// basic arguments
var $type = '';
var $name = '';
var $label = '';
var $is_required = 0;
var $maxlength = 255;
var $minlength = 0;
var $size = 0;


/* PRIVATE PROPERTIES */
var $_value = null;
var $_prompt = '';
var $_html_signal = '';

var $_filename = '';
var $_dirpath = '';


/* ** MAGIC METHODS ** */
// php4 constructor
function CeoFormElementInputText($name, $type, $label, $is_required, $maxlength, $minlength, $debug)
{
  $this->__construct($name, $type, $label, $is_required, $maxlength, $minlength, $debug);
  register_shutdown_function( array($this, '__destruct') );
}
// END constructor

// php5 constructor
function __construct($name, $type, $label, $is_required, $maxlength, $minlength, $debug)
{
  // default
  $this->oid = $type . $name;
  $this->debug = $debug;
  $this->_set_filename();
  $this->_set_dirpath();
  
  // type-specific defaults
  if ( ( $type == 'input_url' ) && empty($maxlength) ) $maxlength = 512;
  if ( ( $type == 'input_email' ) && empty($maxlength) ) $maxlength = 128;
  if ( ( $type == 'input_password' ) && empty($maxlength) ) $maxlength = 64;
  
  // call parent constructor
  parent::__construct($name, $type, $label, $is_required, $maxlength, $minlength, null, null, $this->debug);
  
  // override parent values
  $_TYPES = CeoFormElement::get_valid_types();
  $this->VALID['TYPE'] = $_TYPES['INPUT_TEXT'];
  $this->minlength = $minlength;
  $this->maxlength = $maxlength;
  $this->css_class = 'type_input_text';
}
// END constructor

// destructor
function __destruct()
{
  if ( $this->debug ) $this->print_d("destroying class {$this->class_name} (oid: {$this->oid})");
}
// END destructor



/* ** PUBLIC METHODS ** */
// method: print as block
function print_block($size=12)
{
  $html = '';    // return
  
  // set attributes
  $type = ( $this->type == 'input_password' ) ? 'password' : 'text';
  if ( !empty($size) ) $this->size = $size;
  if ( empty($this->size) ) $this->size = $this->maxlength;
  
  // set prompt class
  $_prompt_class = ( $this->is_valid == 1 ) ? $this->valid_class_name : $this->invalid_class_name;
  
  $html_prompt = ( !empty($this->_prompt) ) ? "<div class=\"prompt\"><span class=\"$_prompt_class\">{$this->_prompt}</span></div>" : '';
  $html_signal = CeoFormElement::static_build_hidden_input($this->_html_signal, 1);
  
  // get value (if password, clear this)
  $value = $this->_get_display_value();
  if ( $this->type == 'input_password' ) $value = '';
  
  
  // core html  
  $core_html = <<<HTML
  
 <label for="{$this->name}">{$this->label}</label>
 <input type="$type" name="{$this->name}" value="{$value}" size="{$this->size}" maxlength="{$this->maxlength}"  />
 {$html_prompt}
 {$html_signal}
HTML;

  // wrap element
  $html = $this->_wrap_print_block($core_html, $this->type, $this->name, $this->id, $this->class);

  return $html;
}



/* ** PRIVATE METHODS ** */
// method: validate input
function _validate_input()
{
  $this->is_valid = 0;
  $_fail = 0;  
  $_prompt = '';
  $_strlen = strlen($this->_value);
  
  if ( $this->debug ) $this->print_d("checking validity of element '{$this->name}'");
  
  // clear valid value
  $this->valid_input = '';
  
  // required
  if ( $this->is_required && empty($this->_value) )
  {
    $_prompt = 'required value &ndash; please fill in';
    $_fail = 1;
  }
  
  // too short
  if ( !$_fail && $this->is_required && ( $_strlen < $this->minlength ) )
  {
    $_prompt = "this is too short (must be at least {$this->minlength} chars)";
    $_fail = 1;
  }
  
  // too long
  if ( !$_fail && ( $_strlen > $this->maxlength ) )
  {
    $_prompt = "this is too long (please limit to {$this->maxlength} chars)";
    $this->_value = substr($this->_value, 0, $this->maxlength);
    $_fail = 1;
  }
  
  // valid email
  if ( !$_fail && !empty($this->_value) && ( $this->type == 'input_email' ) )
  {
    if ( !$this->_is_valid_email($this->_value) )
    {
      $_prompt = 'invalid email format &ndash; please re-enter';
      $_fail = 1;
    }
  }
  
  // valid url
  if ( !$_fail && !empty($this->_value) && ( $this->type == 'input_url' ) ) 
  {
    if ( !$this->_is_valid_url_strict($this->_value) )
    {
      $_prompt = 'invalid web address &ndash; please re-enter';
      $_fail = 1;
    }
  }
  
  // valid password
  if ( !$_fail && !empty($this->_value) && ( $this->type == 'input_password' ) ) 
  {
    // no additional checks for password
  }
  
  // failed?
  if ( !$_fail )
  {
    $this->is_valid = 1;
  }
  else
  {
    $this->is_valid = 0;
    $this->_prompt = $_prompt;    
  }
  
 
  // debug
  if ( $this->debug ) $this->print_d("is valid: {$this->is_valid} -> prompt: {$this->_prompt}");
  
  return;
}
// END method


// method: _set_session_data
function _set_session_data()
{
  // initialize session
  if ( !session_id() ) session_start(); 
  $_SESSION[$this->oid] = array();
  
  // add session data here
  
  return;
}
// END method

// method: get session data
function _get_session_data()
{
  // initialize session
  if ( !$this->_has_session_data() ) return; 
    
  // retrieve session variables
  // $this->var = $_SESSION[$this->oid]['var'];
  
  return;
}
// END method

// method: has session data
function _has_session_data()
{
  // initialize session
  if ( !session_id() ) session_start(); 
    
  // retrieve session variables
  if ( empty($_SESSION[$this->oid]) ) return 0;
  else return 1;
}
// END method


// method: is valid url (strict)
// based on Apeldoorn function: http://www.canowhoopass.com/guides/regex/validateurlsyntax_source.php
function _is_valid_url_strict($url)
{
  $is_valid = 0;  // return
  
  $_RE['scheme'] = "((http://)|(https://)|(ftp://))?";
  $_RE['ip'] = "((((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9]))\.){3}((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9])))";
  $_RE['domain3'] = '([a-zA-Z0-9](([a-zA-Z0-9-]{0,62})[a-zA-Z0-9])?\.)*';
  $_RE['domain2'] = '([a-zA-Z0-9](([a-zA-Z0-9-]{0,62})[a-zA-Z0-9])?\.)';
  $_RE['domain1'] = '([a-zA-Z](([a-zA-Z0-9-]*)[a-zA-Z0-9])?)';
  $_RE['port'] = '(:(([0-5]?[0-9]{1,4})|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5])))?';
  $_RE['path'] = '(/((;)?([a-zA-Z0-9_.!~*' . '\'' . '()-]|(%[0-9a-fA-F]{2})|' . '[:@&=+$,]' . ')+(/)?)*)?';
  $_RE['query'] = '(\?([;/?:@&=+$,]|[a-zA-Z0-9_.!~*' . '\'' . '()-]|(%[0-9a-fA-F]{2}))*)?';
  $_RE['anchor'] = '(#([;/?:@&=+$,]|[a-zA-Z0-9_.!~*' . '\'' . '()-]|(%[0-9a-fA-F]{2}))*)?';
  
  $_RE['address'] = '((' . $_RE['ip'] . ')|(' . $_RE['domain3'] . $_RE['domain2'] . $_RE['domain1'] . '))+';
  $_EREGI['url'] = '^' . $_RE['scheme'] . $_RE['address'] . $_RE['port'] . $_RE['path'] . $_RE['query'] . $_RE['anchor'] . '$';
  
  if ( eregi($_EREGI['url'], $url) ) $is_valid = 1;
  
  return $is_valid;
}

function _set_filename() { $this->_filename = basename(__FILE__); }
function _set_dirpath() { $this->_dirpath = dirname(__FILE__) . $this->DS; }

} // end class
/*____________________________________________________________________________*/

?>
